#include "sk_storage/memmap.hpp"

namespace sk_storage {

MemMap::MemMap() {
  last_log_id_ = 0;
  set_operator_num_ = 0;
  is_immutable_ = false;
}

Status MemMap::Set(uint64_t log_id, const std::string &key,
                   const std::string &value) {
  mp_[key] = value;
  ++set_operator_num_;
  last_log_id_ = log_id;
  if (set_operator_num_ >= MAX_OPERATORS) {
    is_immutable_ = true;
    return Status::MEMMAP_FULL;
  }
  return Status::OK;
}

Status MemMap::Get(const std::string &key, std::string &value) {
  auto it = mp_.find(key);
  if (it == mp_.end()) {
    return Status::KEY_NOT_FOUND;
  }
  value = it->second;
  return Status::OK;
}

bool MemMap::IsImmutable() const { return is_immutable_; }

std::map<std::string, std::string> &MemMap::GetSnapshot() { return mp_; }

MemMap::~MemMap() {}

} // namespace sk_storage
